# -*- coding: utf-8 -*-
class CustomerTypeController < ApplicationController
  before_filter :employee_authorize
  before_filter :sys_before_log
  before_filter :employee_action_hx
  before_filter :privilege_filter,:except=>[:newCustomerType,:editCustomerType,:customerTypeLock]
  
  def customerTypeList
    
  end

  def create
    begin
      customerType = CustomerType.new
      customerType.company_id =  session[:company_id]
      customerType.department_id =  session[:department_id]
      customerType.employee_id =  session[:employee_id]
      customerType.name = params[:name]
      customerType.save!
      info = "成功"
    rescue Exception => e
      info = e.to_s
      logger.info info
      DbLog.info(session[:company_id], session[:department_id], session[:employee_id], params[:controller], params[:action], request.remote_ip ,e.to_s)
    end

    result = "{success:true,info:'#{info}'}"
    render :text=>result, :layout=>false
  end

  def update
    begin
      customerType = CustomerType.find(params[:id])
      customerType.employee_id =  session[:employee_id]
      customerType.name = params[:name]
      customerType.save!
      info = "成功"
    rescue Exception => e
      info = e.to_s
      logger.info info
      DbLog.info(session[:company_id], session[:department_id], session[:employee_id], params[:controller], params[:action], request.remote_ip ,e.to_s)
    end

    result = "{success:true,info:'#{info}'}"
    render :text=>result, :layout=>false
  end

  #锁定或解锁销售类型
  def customerTypeLock
    customerType = CustomerType.find(params[:id])
    if customerType.is_locked == 1
      customerType.is_locked = 0
    else
      customerType.is_locked = 1
    end
    customerType.save!
    info = "成功"
    result = "{success:true,info:'#{info}'}"
    render :text=>result, :layout=>false
  end
  
  #锁定或解锁客户类型
  def customerTypeLock
    customerType = CustomerType.find(params[:id])
    if customerType.is_locked == 1
      customerType.is_locked = 0
    else
      customerType.is_locked = 1
    end
    customerType.save!
    info = "成功"
    result = "{success:true,info:'#{info}'}"
    render :text=>result, :layout=>false
  end
  
  def getAllCustomerTypes
    forCBB = params[:for_cbb]
    if forCBB == 'true'
      customerTypes = CustomerType.find(:all,:conditions=>["is_locked = ?",0])
    else
      customerTypes = CustomerType.find(:all)
    end    
    jsonStr="{totalProperty:"+customerTypes.length.to_s+",root:"+customerTypes.to_json()+"}"
    logger.info jsonStr
    render :text=>jsonStr, :layout=>false
  end

  def getCustomerTypeById
    customerType = CustomerType.find(params[:id])
    rootStr = customerType.to_json()
    jsonStr = "{root:["+rootStr+"]}"
    logger.info jsonStr
    render :text=>jsonStr, :layout=>false
  end
end
